package cn.codewen.servlet;

import cn.codewen.pojo.User;
import cn.codewen.service.UserService;
import cn.codewen.service.impl.CosServiceImpl;
import cn.codewen.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/cos")
@MultipartConfig
public class CosServlet extends HttpServlet {


    //0. 创建userService对象
    // private AdminService adminService=new AdminServiceImpl();
//    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 设置字符集
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        //2. 接收参数
        String m = request.getParameter("m");
        //3. 判断
   /*     if (m.equals("list")) {
            list(request, response);
        } else if (m.equals("add")) {
            add(request, response);
        } else if (m.equals("edit")) {
            edit(request, response);
        } else if (m.equals("del")) {
            del(request, response);
        }*/

        switch (m) {
            case "list":
                list(request, response);
                break;
            case "add":
                add(request, response);
                break;
            case "edit":
                edit(request, response);
                break;
            case "del":
                del(request, response);
                break;
            case "addDir":
                addDir(request, response);
                break;
            case "addFile":
                addFile(request, response);
                break;
            case "moveFile":
                moveFile(request, response);
                break;
        }

    }

    private void moveFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        if (request.getMethod().equals("POST")) {


            //1.接收参数
            User user = (User) request.getSession().getAttribute("user");

            String prefix = request.getParameter("prefix");// prefix=zwj/

            if (prefix == null) {
                prefix = "";
            }

            String oldFile = request.getParameter("file");// file=codewen.jpg
            String newFile = request.getParameter("newFile");// file=codewen.jpg


            String oldKey = user.getUsername() + "/" + prefix + oldFile;
            String newKey = user.getUsername() + "/" + prefix + newFile;
            //2.处理数据
            CosServiceImpl cosService = new CosServiceImpl();

            cosService.moveFile(oldKey,newKey);

            //3. 跳转
            response.sendRedirect("/cos?m=list&prefix=" + prefix);





        }else {

            request.getRequestDispatcher("/moveFile.jsp").forward(request,response);

        }


    }

    private void addFile(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        //1.判断提交方式
        if (request.getMethod().equals("POST")) {
            //1. 接收参数
            User user = (User) request.getSession().getAttribute("user");

            String prefix = request.getParameter("prefix");// prefix=zwj/

            if (prefix == null) {
                prefix = "";
            }

            //取到上传文件的对象
            Part file = request.getPart("file");
            //文件流
            InputStream inputStream = file.getInputStream();
            System.out.println(inputStream);
            //文件的类型
            String contentType = file.getContentType();
            System.out.println(contentType);
            //文件名
            String fileNameOk = file.getSubmittedFileName();
            System.out.println(fileNameOk);


            //   D://aaa/codewen.jpg
            //String fileName = request.getParameter("file");

          /*  //通过/分割
            String[] fileNames = fileName.split("/");
            //取数组最后1个元素
            String fileNameOk = fileNames[fileNames.length - 1];   //codewen.jpg
*/
            //最最终的路径
            String key = user.getUsername() + "/" + prefix + fileNameOk;
            //2. 处理数据
            //2.1 创建cosService对象
            CosServiceImpl cosService = new CosServiceImpl();
            //2.2 调用
            Boolean rs = cosService.addFileSteam(key, inputStream, contentType);
            // 跳转
            if (rs) {

                response.sendRedirect("/cos?m=list&prefix=" + prefix);
            }


        } else {

            request.getRequestDispatcher("/addFile.jsp").forward(request, response);

        }
    }

    private void addDir(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        if (request.getMethod().equals("POST")) {
            //创建目录
            //1. 接收参数
            User user = (User) request.getSession().getAttribute("user");
            String prefix = request.getParameter("prefix");

            if (prefix == null) {
                prefix = "";
            } else {
                prefix = new String(prefix.getBytes("iso-8859-1"), "UTF-8");
            }

            //prefix=111/
            //username=codewen
            //dir=zwj

            //最终在系统里创建的目录： codewen+"/"+111/+zwj+"/"
            String userNamePrefix = user.getUsername() + "/" + prefix + "/";
            //需要创建的目录名
            String dirName = request.getParameter("dir") + "/";
            System.out.println(dirName);
            //最终的目录
            String key = userNamePrefix + dirName;
            System.out.println("++++++++++++++++++");
            System.out.println(key);
            System.out.println("++++++++++++++++++");
            //2. 处理数据
            //2.1 创建Service对象
            CosServiceImpl cosService = new CosServiceImpl();
            //2.2 调用addDir方法
            Boolean rs = cosService.addDirs(key);
            //3. 跳转
            if (rs) {

                response.sendRedirect("/cos?m=list&prefix=" + prefix);
            }

        } else {
            //展示
            request.getRequestDispatcher("/addDir.jsp").forward(request, response);
        }


    }


    private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //1.接收参数
        User user = (User) request.getSession().getAttribute("user");

        String prefix = request.getParameter("prefix");// prefix=zwj/

        if (prefix == null) {
            prefix = "";
        }

        String file = request.getParameter("file");// file=codewen.jpg


        String key = user.getUsername() + "/" + prefix + file;
        //2.处理数据
        CosServiceImpl cosService = new CosServiceImpl();

        cosService.delFile(key);

        //3. 跳转
        response.sendRedirect("/cos?m=list&prefix=" + prefix);


    }

    private void edit(HttpServletRequest request, HttpServletResponse response) {
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //1.判断提交方式
        if (request.getMethod().equals("POST")) {
            //1. 接收参数
            User user = (User) request.getSession().getAttribute("user");

            String prefix = request.getParameter("prefix");// prefix=zwj/

            if (prefix == null) {
                prefix = "";
            }

            //   D://aaa/codewen.jpg
            String fileName = request.getParameter("file");

            //通过/分割
            String[] fileNames = fileName.split("/");
            //取数组最后1个元素
            String fileNameOk = fileNames[fileNames.length - 1];   //codewen.jpg

            //最最终的路径
            String key = user.getUsername() + "/" + prefix + fileNameOk;
            //2. 处理数据
            //2.1 创建cosService对象
            CosServiceImpl cosService = new CosServiceImpl();
            //2.2 调用
            Boolean rs = cosService.addFile(fileName, key);
            // 跳转
            if (rs) {

                response.sendRedirect("/cos?m=list&prefix=" + prefix);
            }


        } else {

            request.getRequestDispatcher("/addFile.jsp").forward(request, response);

        }


    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. 接收参数

        //取到user
        User user = (User) request.getSession().getAttribute("user");

        String username = user.getUsername();

        String prefix = request.getParameter("prefix");

        if (prefix == null) {
            prefix = "";
        }

        //拼接上用户名的目录  prefix="";    ====>  codewen/
        // prefix="aaa/"                 ===>   codewen /  aaa/
        String userNamePrefix = user.getUsername() + "/" + prefix;

        //wjx/
        //2. 处理数据

        CosServiceImpl cosService = new CosServiceImpl();


        List listObjects = cosService.listObjects(userNamePrefix);


        //3. 展示或跳转
        //3.1 传递数据
        request.setAttribute("prefix", prefix);// prefix=cloud01/
        request.setAttribute("userNamePrefix", userNamePrefix); // codewen+"/"+cloud01/
        request.setAttribute("username", username);
        request.setAttribute("listObjects", listObjects);
        //3.2 转发请求
        request.getRequestDispatcher("/list.jsp").forward(request, response);


    }


}
